//
//  AppeptGroupVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/8.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AppeptGroupVC.h"
#import "AppectGroupCell.h"
#import "AppeptNewGroupVC.h"
#import "GroupInfoVC.h"
#import "AppectCroupModel.h"

@interface AppeptGroupVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) AppectGroupCell *appectGroupCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) CJButton *btnAddGroup; //
@end

@implementation AppeptGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"承运分组")];
    [self setBackLeftButton:@""];

    self.appectGroupCell = [[AppectGroupCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.btnAddGroup];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/vehicleTeam",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                            @"pageSize":@"10",
                             @"acceptType":@"1"
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }

            NSError * error ;
            NSArray *arr = [AppectCroupModel arrayOfModelsFromDictionaries:list error:&error];
            [ws.modelArray addObjectsFromArray:arr];

            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
//        }else{

//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        
        [ws.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"AppectGroupCell";
    AppectGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[AppectGroupCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    AppectCroupModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];

    WS(ws);
    cell.blockDelect = ^(id anyObject) {
        [ws delectAppectGroupWithCode:model];
    };
    cell.blockChange = ^(id anyObject) {
        [ws changeAppectGroupWithCode:model.code withTitle:model.name];
    };
    return cell;
}

- (void)changeAppectGroupWithCode:(NSString *)code withTitle:(NSString *)title{
    GroupInfoVC *vc = [[GroupInfoVC alloc]init];
    vc.strCode = code;
    vc.strTitle = title;
    [self pushVc:vc];
}

- (void)delectAppectGroupWithCode:(AppectCroupModel *)model{

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/vehicleTeam/batchDelTeam",CJBaseUrl);
    NSDictionary *dicPra = @{@"teamCodes":model.code,
                             };
    WS(ws);
    CJAlertView *alert = [[CJAlertView alloc]initWithTitle:@"删除提醒" strContent:CJStringWithFormat(@"您确定要删除%@吗?",model.name) ClickOnSure:^(NSString * _Nonnull strSure) {
        
       [[HDNetworking sharedHDNetworking] DELETE:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
           
            [ws.tableView.mj_header beginRefreshing];

       } failure:^(NSError * _Nonnull error) {
           DLog(@"++++++++%@",error);
       }];
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {
       
    }];
    
    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss=YES;
    [alertView show];
}

- (void)addAppectGoud:(CJButton *)sender{
    
    WS(ws);
    AppeptNewGroupVC *vc = [[AppeptNewGroupVC alloc]init];
    vc.blockSuccess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

- (CJButton *)btnAddGroup{
    if (!_btnAddGroup) {
        _btnAddGroup = CJButton.btnInit
        .btnFrame(CGRectMake(0, kScreenHeight - CJBottomBtnHeight, kScreenWidth,CJBottomBtnHeight))
        .btnText(@"新增承运组")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnTargetAction(self,@selector(addAppectGoud:))
        .btnFontSize(17)
        ;
    }
    return _btnAddGroup;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - _btnAddGroup.height - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}

@end
